package com.study.www.config;
import	java.util.Map;

import com.alibaba.druid.pool.xa.DruidXADataSource;
import com.atomikos.jdbc.AtomikosDataSourceBean;
import com.mysql.jdbc.jdbc2.optional.MysqlXADataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * 多数据源：数据源1
 * @author : 89007337.zhuyanpeng
 **/
@Configuration
public class Test2DataSourceConfig {



    public static final String TEST2SQLSESSIONFACTORY="test2SqlSessionFactory";

    /**
     * 数据源
     */
    @Bean
    public DataSource test2DataSource(DBConfig2 config2) throws SQLException {
        MysqlXADataSource mysqlXADataSource = new MysqlXADataSource();
        mysqlXADataSource.setUrl(config2.getUrl());
        //mysqlXADataSource.setPinGlobalTxToPhysicalConnection(true);
        mysqlXADataSource.setUser(config2.getUsername());
        mysqlXADataSource.setPassword(config2.getPassword());
        //Atomikos 事务
        AtomikosDataSourceBean atomikosDataSourceBean = new AtomikosDataSourceBean();
        atomikosDataSourceBean.setXaDataSource(mysqlXADataSource);
        atomikosDataSourceBean.setMaxPoolSize(config2.getMaxPoolSize());
        atomikosDataSourceBean.setMinPoolSize(config2.getMinPoolSize());
        atomikosDataSourceBean.setUniqueResourceName("test2DataSource");
        System.out.println(mysqlXADataSource.getClass().getName());
        atomikosDataSourceBean.setXaDataSourceClassName(mysqlXADataSource.getClass().getName());
        return atomikosDataSourceBean;
    }

    /**
    *  SqlSession 工厂
    */
    @Bean(TEST2SQLSESSIONFACTORY)
    public SqlSessionFactory test2SqlSessionFactory(@Value("${mybatis.test2.mapperLocations}")  String test2MapperLocations,
                                                    @Value("${mybatis.test2.type-aliases-package}")  String test2TypeAliasesPackage,
                                                    @Value("${mybatis.config-location}")  String configLocation,
                                                    @Qualifier("test2DataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources(test2MapperLocations)
        );
        //mybatis Config 配置
        ClassPathResource classPathResource = new ClassPathResource(configLocation);
        sqlSessionFactoryBean.setConfigLocation(classPathResource);
        sqlSessionFactoryBean.setTypeAliasesPackage(test2TypeAliasesPackage);
        return sqlSessionFactoryBean.getObject();
    }

}
